package com.lwq.mall.seckill.schedule;

import com.lwq.mall.seckill.service.SeckillSkuService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 定时上架商品任务
 *
 */
@Slf4j
@Component
@EnableScheduling
@EnableAsync
public class SeckillSkuSchedule {

    @Autowired
    SeckillSkuService seckillSkuService;
    @Autowired
    RedissonClient redissonClient;
    @Async
    @Scheduled(cron = "0 * * * * *")
    public void uploadSeckillSku3day(){
        //分布式锁
        RLock lock = redissonClient.getLock("seckill:upload:lock");
        System.out.println("lock = " + lock);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillSkuService.uploadSeckillSku3day();
        } catch (Exception e) {
            log.info("定时获取秒杀活动数据异常："+e);
        }finally {
            lock.unlock();
        }
    }


}
